package com.taotao.service.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EUDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.IDUtils;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemExample;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;
	@Override
	public TbItem getItemById(long id) {
		// TODO Auto-generated method stub
		//TbItem item=itemMapper.selectByPrimaryKey(id);
		TbItemExample example=new TbItemExample();
		example.createCriteria().andIdEqualTo(id);
		List<TbItem> selectByExample = itemMapper.selectByExample(example);
		if(selectByExample==null ||selectByExample.isEmpty() ) {
			return null;
		}
		return selectByExample.get(0);
	}

	
	@Override
	public EUDataGridResult getItemList(int page, int rows) {
		//分页查询
		TbItemExample example=new TbItemExample();
		PageHelper.startPage(page, rows);
		List<TbItem> items=itemMapper.selectByExample(example);
		//获取信息
		PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(items);
		
		//构造结果
		EUDataGridResult euDataGridResult = new EUDataGridResult();
		euDataGridResult.setTotal(pageInfo.getTotal());
		euDataGridResult.setRows(items);
		return euDataGridResult;
	}


	@Override
	public TaotaoResult createItem(TbItem item,String desc,String itemParams) throws Exception {
		Date date = new Date();
		//获得商品id
		long id = IDUtils.genItemId();
		//添加商品信息
		item.setId(id);
		//商品状态，1-正常，2-下架，3-删除
		item.setStatus((byte) 1);
		item.setCreated(date);
		item.setUpdated(date);
		itemMapper.insert(item);

		TaotaoResult insertDesc = insertDesc(item.getId(), desc);
		if(insertDesc.getStatus()!=200) {
			throw new Exception("插入商品描述失败");
		}
		if(itemParams==null || itemParams.trim().isEmpty()) {
			//。。。。
		}
		else{
			TaotaoResult paramItemResult = insertItemParamItem(item.getId(), itemParams);
			if(paramItemResult.getStatus()!=200)
				throw new Exception("插入商品规格失败");
		}
		return TaotaoResult.ok();
	}
	
	private TaotaoResult insertDesc(Long itemId,String desc) {
		TbItemDesc record=new TbItemDesc();
		record.setItemId(itemId);
		record.setItemDesc(desc);
		Date date = new Date();
		record.setCreated(date);
		record.setUpdated(date);
		itemDescMapper.insert(record);
		return TaotaoResult.ok();
	}
	
	private TaotaoResult insertItemParamItem(Long itemId,String itemParams) {
		TbItemParamItem itemParamItem = new TbItemParamItem();
		Date date = new Date();
		itemParamItem.setItemId(itemId);
		itemParamItem.setParamData(itemParams);
		itemParamItem.setUpdated(date);
		itemParamItem.setCreated(date);
		itemParamItemMapper.insert(itemParamItem);
		return TaotaoResult.ok();
	}


	
	

}
